package com.gt.quality.system.service.excel;

import cn.hutool.crypto.SecureUtil;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.read.listener.ReadListener;
import cn.idev.excel.util.ListUtils;
import com.gt.quality.system.entity.SysUser;
import com.gt.quality.system.mapper.SysUserMapper;
import com.gt.quality.system.service.excel.user.ExcelExportUserInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @description: 用户导入监听器
 * @author: makeJava
 * @createTime: 2025-01-01 17:14
 * @since JDK 21
 **/
@Slf4j
public class SysUserExcelInputListener implements ReadListener<ExcelExportUserInfo> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;

    /**
     * 缓存的数据
     */
    private List<SysUser> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    /**
     * 假设这个是一个DAO，当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。
     */
    private final SysUserMapper sysUserMapper;


    public SysUserExcelInputListener(SysUserMapper sysUserMapper) {
        // 这里是demo，所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
        this.sysUserMapper = sysUserMapper;
    }


    /**
     * 每一条数据解析都会来调用
     *
     * @param data            解析的数据
     * @param analysisContext analysisContext
     */
    @Override
    public void invoke(ExcelExportUserInfo data, AnalysisContext analysisContext) {
        log.info(data.toString());
        data.setPassword(SecureUtil.md5(data.getPassword()));
        SysUser sysUser = getSysUser(data);
        cachedDataList.add(sysUser);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    private SysUser getSysUser(ExcelExportUserInfo data) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(data.getUserName());
        sysUser.setNickName(data.getNickName());
        sysUser.setUserType(data.getUserType());
        sysUser.setEmail(data.getEmail());
        sysUser.setPhoneNumber(data.getPhoneNumber());
        sysUser.setSex(data.getSex());
        sysUser.setAvatar(data.getAvatar());
        sysUser.setPassword(data.getPassword());
        sysUser.setStatus(data.getStatus());
        sysUser.setRemark(data.getRemark());
        sysUser.setDeptId(103L);
        sysUser.setCreateDept(103L);
        sysUser.setTenantId("000000");
        sysUser.setDelFlag("0");
        sysUser.setUpdateBy(1L);
        return sysUser;
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param analysisContext context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
        if (cachedDataList == null) {
            return;
        }
        sysUserMapper.insertBatchSomeColumn(cachedDataList);
        log.info("存储数据库成功！");
    }
}
